import Icon from "@ui/RJMart/icons";
import EventApi from "@module/EventApi";
import ZenModal from "@module/../components/ZenUI/ZenModal/ZenModal";
import urlHook from "@page/common/utils/urlHook";
import BreadCrumbs from "./../../components/BreadCrumbs";
import SearchTab from "./comps/SearchTab/SearchTab";
import Table from "./comps/Table/Table";
import "./style.scss";
import REST from "@module/Rest";
import ic_notfound from '@assets/ic_notfound.png'
import usePartState from "@module/usePartState";
import ToastUtils from '@page/common/utils/ToastUtils'


export default function BidList(props) {
    //定义全局变量 const [scope值，更新方法] = React.useState({初始化的值});
    const [currentDate, updateCurrentDate] = usePartState({
        selectName: "",
        lists:[],
        totalPage:1,
        currentPage:1,
        hasLoadData:false,
        tabIndex:1,
        searchText:""
    });

    const {lists,currentPage,totalPage,hasLoadData,tabIndex}=currentDate;

    React.useEffect(() => {
        getProcurements();
    }, [tabIndex,currentPage]);



    function getProcurements() {
        const {tabIndex,currentPage,searchText}=currentDate
        let url=`/bid/anonymous/order/item/getProcurements`;
        let params= {
            status: tabIndex==2? 0:1,
            page:currentPage>0?currentPage:1,
        }
        let suppId=Uts.getCookie('suppId');
        let orgId=Uts.getCookie('orgId');
        if(suppId){
            params.suppId=parseInt(suppId) ;
        }
        if(orgId){
            params.orgId=parseInt(orgId);
        }
        if(searchText){
            params.search=searchText;
        }

        ToastUtils.loading()
        REST.post(url,params)
            .then(res=>{
                console.log(res)
                ToastUtils.hide()
                if(res.resultCode=='0000'){
                    updateCurrentDate({lists:res.entities,totalPage:res.totalPage,currentPage:res.currentPage?res.currentPage:1,hasLoadData:true})
                }
            })
            .catch(err=>{
                ToastUtils.hide()
                console.log(err)
            })
    }


    function changeTab(index) {
        updateCurrentDate({tabIndex:index,currentPage:1,searchText:""});
    }

    function changePage(page) {
        updateCurrentDate({currentPage:page});
    }

 
    //更新视图数据 私有的方法前缀用_
    function _updateData(value) {
        console.log(value);
        let newValue = { ...currentDate, ...value };
        updateCurrentDate(newValue);
    }



    function doSearchText() {
        updateCurrentDate({
            currentPage:1
        })
        setTimeout(()=>{
            getProcurements()
        },300)
    }



    return (
        <div className="zen_flex rj_bidlist-wp">
            <div className="zen_w-1200">
                <BreadCrumbs tittleList={[{ name: "在线竞价" }, { name:tabIndex==1? "竞价中":"已结束" }]}/>
                <SearchTab
                    changeTab={(index)=>changeTab(index)}
                    changeText={(txt)=>{
                        updateCurrentDate({
                            searchText:txt
                        })
                    }}
                    doSearchText={doSearchText}
                />
                {
                    lists&&lists.length==0&&hasLoadData ?
                        <Empty/>
                        :
                        <Table
                            lists={lists}
                            currentPage={currentPage}
                            totalPage={totalPage}
                            changePage={changePage}
                            tabIndex={tabIndex}
                        />  
                }
               
            </div>
        </div>
    );
}


function Empty() {

    return(
        <div className={'bidlist-empty'}>
            <img src={ic_notfound} alt=""/>
            <p>暂无竞价消息~</p>
        </div>
    )
}